// ObjectList.cpp: implementation of the ObjectList class.
//
//////////////////////////////////////////////////////////////////////

#include "ObjectList.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ObjectList::ObjectList()
{
	this->list = new ulLinkedList();
}

ObjectList::~ObjectList()
{

}

int ObjectList::add(ssgTransform* t,int type, int kind, float x, float y, float z)
{
	// create slot
	ObjectSlot *newnode = (ObjectSlot*) malloc(sizeof(ObjectSlot)); 
	newnode->x = x;
	newnode->y = y;
	newnode->z = z;
	newnode->h = 0.0f;
	newnode->p = 0.0f;
	newnode->r = 0.0f;
	newnode->type = type;
	newnode->kind = kind;
	//newnode->code = this->code; this->code++;

	// add slot
	list->appendNode(newnode);

	// return node code
	return list->getNumNodes() - 1;
}

int ObjectList::add(ssgTransform* t,int type, int kind, float x, float y, float z, float h, float p, float r)
{
	// create slot
	ObjectSlot *newnode = (ObjectSlot*) malloc(sizeof(ObjectSlot)); 
	//printf("2\n");
	newnode->x = x;
	newnode->y = y;
	newnode->z = z;
	newnode->h = h;
	newnode->p = p;
	newnode->r = r;
	newnode->type = type;
	newnode->kind = kind;
	//newnode->code = this->code; this->code++;
	newnode->t = t;

	//printf("Adding (%d)(%d)(%p) %f %f %f %f %f %f\n",type,kind,t,x,y,z,h,p,r);

	// add slot
	list->appendNode(newnode);

	// return node code
	return list->getNumNodes() - 1;
}

int ObjectList::add(int type, int kind, float x, float y, float z, float h, float p, float r)
{
	// create slot
	ObjectSlot *newnode = (ObjectSlot*) malloc(sizeof(ObjectSlot)); 
	//printf("2\n");
	newnode->x = x;
	newnode->y = y;
	newnode->z = z;
	newnode->h = h;
	newnode->p = p;
	newnode->r = r;
	newnode->type = type;
	newnode->kind = kind;
	//newnode->code = this->code; this->code++;

	//printf("Adding (%d)(%d) %f %f %f %f %f %f\n",type,kind,x,y,z,h,p,r);

	// add slot
	list->appendNode(newnode);

	// return node code
	return list->getNumNodes() - 1;
}

ObjectSlot* ObjectList::get(int post)
{
	//printf("numnodes: %d\n",list->getNumNodes());
	ObjectSlot *os = (ObjectSlot*)(list->getNodeData(post));
	return os;
}

int ObjectList::set(int posi, float x, float y, float z, float h, float p, float r)
{
	ObjectSlot* os = (ObjectSlot*)list->getNodeData(posi);
	
	if(os != NULL) 
	{
		os->x = x;
		os->y = y;
		os->z = z;
		os->h = h;
		os->p = p;
		os->r = r;
		return 1;
	}
	else
	{
		return 0;
	}
}

void ObjectList::fprint(FILE* f)
{
	int numnodes;

	numnodes = this->list->getNumNodes();

	fprintf(f,"%d\n",numnodes);

	for(int i = 0; i < numnodes; i++)
	{
	ObjectSlot *os = (ObjectSlot*)list->getNodeData(i);
	fprintf(f,"%d %d %f %f %f %f %f %f\n",os->type,os->kind,os->x,
															os->y,
															os->z,
															os->h,
															os->p,
															os->r);
/*	printf("%d %d %f %f %f %f %f %f\n",os->type,os->kind,os->x,
															os->y,
															os->z,
															os->h,
															os->p,
															os->r);*/
	}
}

void ObjectList::load(FILE* f)
{
	int n,t,k;
	char c;
	float x,y,z,h,p,r;

	fscanf(f,"%d%c",&n,&c);
	for(int i = 0; i < n; i++)
		{
		fscanf(f,"%d%d%f%f%f%f%f%f%c",&t,&k,&x,&y,&z,&h,&p,&r,&c);
		this->add(t,k,x,y,z,h,p,r);
		}
}

int ObjectList::getSize()
{
	return list->getNumNodes();
}

void ObjectList::remove(ssgTransform* t)
{
	bool deleted = false;
	int numnodes = this->list->getNumNodes();
	int i = 0;

	/*printf("Trying to remove (%d)",numnodes);
	printf("(%p) ",t);
	if(t != NULL) printf("%s ...",t->getName());*/


	while(!deleted && i<numnodes)
		{
		ObjectSlot* os = (ObjectSlot*)(this->list->getNodeData(i));
		if(os->t == t)
			{
			this->list->removeNode(i);
			deleted = true;
			}
		i++;
		}
	/*numnodes = this->list->getNumNodes();
	if(!deleted) printf(" not found! (%d)\n",numnodes);
	else printf(" done! (%d)\n",numnodes);*/
}

void ObjectList::set(ssgTransform* t,int i)
{
	//printf("i: %d\n",i);
	ObjectSlot* os = (ObjectSlot*)(this->list->getNodeData(i));
	os->t = t;
	//printf("Set transform (%d)(%p) %f %f %f %f %f %f\n",i,os->t,os->x,os->y,os->z,os->h,os->p,os->r);
}
